<?php

class Admin_MenuController extends Zend_Controller_Action {

    public function init() {
        if (!Zend_Auth::getInstance()->hasIdentity()) {
            $this->_redirect("/admin/home");
        }
        $this->_helper->layout->setLayout("admin");
    }

    public function indexAction() {
        $form = new Admin_Form_Menuadd();
        $this->view->form = $form;
        $sysMenu = new System_Menu();
        $this->view->fullMenu = $sysMenu->getMenuArray("all"); #visibled or all
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $values = $form->getValues();
                $menuName = $values['name'];
                $menuLink = '';
                $menuVisible = $values['visible'];
                $menuIcon = '';
                $menuType = 'parent';
                $menuParentId = '0';
                if ($values['option'] != 0) {
                    $menuParentId = $values['option'];
                    $menuType = 'sub';
                }
                $db = new Admin_Model_Menu();
                $db->createNewMenu(array(
                    'name' => $menuName,
                    'link' => $menuLink,
                    'visible' => $menuVisible,
                    'icon' => $menuIcon,
                    'type' => $menuType,
                    'parent_id' => $menuParentId
                ));
                $this->_redirect("/admin/menu/add");
            }
        }
    }

    public function updateAction() {
        $id = $this->getinputidAction();
        $form = new Admin_Form_MenuUpdate();
        $form->main($id);
        $this->view->form = $form;
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $values = $form->getValues();
                $menuName = $values['name'];
                $menuLink = '';
                $menuVisible = $values['visible'];
                $menuIcon = '';
                $menuType = 'parent';
                $menuParentId = '0';
                if ($values['option'] != 0) {
                    $menuParentId = $values['option'];
                    $menuType = 'sub';
                }
                $db = new Admin_Model_Menu();
                $data = array(
                    'name' => $menuName,
                    'link' => $menuLink,
                    'visible' => $menuVisible,
                    'icon' => $menuIcon,
                    'type' => $menuType,
                    'parent_id' => $menuParentId
                );
                $db->updateMenu($data, $values['id']);
                $this->_redirect("/admin/menu/add");
            }
        }
    }

    /**
     * Get ID.    
     *
     */
    public function getinputidAction() {
        $filters = array(
            'id' => array('HtmlEntities', 'StripTags', 'StringTrim')
        );
        $validators = array(
            'id' => array('Int')
        );
        $input = new Zend_Filter_Input($filters, $validators);
        $input->setData($this->getRequest()->getParams());
        $id = $input->id; //id
        return $id;
    }

}

